/*******************************************************************************
 * Copyright (c) 2014 Francesco Gabbrielli.
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Francesco Gabbrielli - initial API and implementation
 ******************************************************************************/
package it.francescogabbrielli.android.solidalapp;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class GCMReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {

		SolidalApp app = (SolidalApp) context.getApplicationContext();
		
		synchronized (app) {
			
			Log.i(SolidalApp.TAG, "GCM receiving: "+intent);
			
			switch(IntentAction.valueOf(intent)) {
			
			case GCM_REGISTER_DEVICE:
				String id = intent.getStringExtra(Constants.REGISTRATION_ID);
				if (id!=null) {
					String old = app.getStatus().getGcmId(); 
					if (!id.equals(old)) {
						app.getStatus().setGcmId(id);
						app.register();
					}
				}
				break;
				
			default:
			}
		}

	}
}
